package ch.ebnoether.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ch.ebnoether.controller.*;
import ch.ebnoether.wuerfel.Dice;

public class WinnerCardLeft extends JPanel {

  private static final long serialVersionUID = 6330262583183381568L;

  @SuppressWarnings("unused")
  private JLabel one;
  @SuppressWarnings("unused")
  private JLabel two;
  @SuppressWarnings("unused")
  private JLabel three;
  @SuppressWarnings("unused")
  private JLabel four;
  @SuppressWarnings("unused")
  private JLabel five;
  @SuppressWarnings("unused")
  private JLabel six;
  @SuppressWarnings("unused")
  private JLabel total;
  @SuppressWarnings("unused")
  private JLabel addition;
  @SuppressWarnings("unused")
  private JLabel completeUp;
  private JLabel[] labels;

  private JTextField oneField;
  private JTextField twoField;
  private JTextField threeField;
  private JTextField fourField;
  private JTextField fiveField;
  private JTextField sixField;
  private JTextField totalField;
  private JTextField additionField;
  private JTextField completeUpField;
  private JTextField[] textFields;

  private Font fontLabel;
  private Font fontTextField;

  private Dice[] dices;
  private YatzyGUI yatzi;
  private WinnerCardRight right;

  private int totalLeft;
  public static int gesamtLinks;

  public WinnerCardLeft(Dice[] dices, WinnerCardRight rechts, YatzyGUI yatzi) {
    gesamtLinks = 0;
    this.totalLeft = 0;
    this.dices = dices;
    this.right = rechts;
    this.yatzi = yatzi;
    this.labels = new JLabel[9];
    this.textFields = new JTextField[9];

    this.labels[0] = this.one = new JLabel("1er");
    this.labels[1] = this.two = new JLabel("2er");
    this.labels[2] = this.three = new JLabel("3er");
    this.labels[3] = this.four = new JLabel("4er");
    this.labels[4] = this.five = new JLabel("5er");
    this.labels[5] = this.six = new JLabel("6er");
    this.labels[6] = this.total = new JLabel("Total");
    this.labels[7] = this.addition = new JLabel("Bonus");
    this.labels[8] = this.completeUp = new JLabel("Total 1");

    this.textFields[0] = this.oneField = new JTextField();
    this.textFields[1] = this.twoField = new JTextField();
    this.textFields[2] = this.threeField = new JTextField();
    this.textFields[3] = this.fourField = new JTextField();
    this.textFields[4] = this.fiveField = new JTextField();
    this.textFields[5] = this.sixField = new JTextField();
    this.textFields[6] = this.totalField = new JTextField();
    this.textFields[7] = this.additionField = new JTextField();
    this.textFields[8] = this.completeUpField = new JTextField();

    this.fontLabel = new Font("schrift", Font.BOLD, 16);
    this.fontTextField = new Font("schrift", Font.BOLD, 16);

    formatField();
    producingPanel();
    setMouseListener();
  }

  private void formatField() {

    for (int i = 0; i < this.labels.length; i++) {
      setFormatLabel(this.labels[i]);
      setFormatField(this.textFields[i]);
    }

    this.totalField.setText("" + this.totalLeft);
    this.completeUpField.setText("" + gesamtLinks);
  }

  private void producingPanel() {
    this.setLayout(new GridLayout(9, 2, 5, 5));

    for (int i = 0; i < this.labels.length; i++) {
      this.add(this.labels[i]);
      this.add(this.textFields[i]);
    }
  }

  private void setMouseListener() {
    this.oneField.addMouseListener(new EinerController(dices, 1, this, right, yatzi));
    this.twoField.addMouseListener(new EinerController(dices, 2, this, right, yatzi));
    this.threeField.addMouseListener(new EinerController(dices, 3, this, right, yatzi));
    this.fourField.addMouseListener(new EinerController(dices, 4, this, right, yatzi));
    this.fiveField.addMouseListener(new EinerController(dices, 5, this, right, yatzi));
    this.sixField.addMouseListener(new EinerController(dices, 6, this, right, yatzi));
  }

  private void setFormatField(JTextField f) {
    f.setEditable(false);
    f.setFont(this.fontTextField);
    f.setHorizontalAlignment(JTextField.CENTER);
  }

  private void setFormatLabel(JLabel l) {
    l.setFont(this.fontLabel);
  }

  public void updatTotalLeft() {
    this.totalField.setText("" + this.totalLeft);
    if (totalLeft >= 63) {
      gesamtLinks = this.totalLeft + 35;
      this.additionField.setText("" + 35);
      this.additionField.setForeground(Color.RED);
    } else {
      gesamtLinks = this.totalLeft;
    }
    this.completeUpField.setText("" + gesamtLinks);
  }

  public int getTotalLeft() {
    return this.totalLeft;
  }

  public void setTotalLeft(int totalLinks) {
    this.totalLeft = totalLinks;
  }
}
